home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch8 / Stocks1.frm (.txt) < prev    next >
Visual Basic Form  |  1999-05-27  |  5KB  |  164 lines

  1. VERSION 5.00
  2. Begin VB.Form frmStocks1 
  3.    Caption         =   "Stocks1"
  4.    ClientHeight    =   3990
  5.    ClientLeft      =   1875
  6.    ClientTop       =   1380
  7.    ClientWidth     =   5190
  8.    LinkTopic       =   "Form1"
  9.    PaletteMode     =   1  'UseZOrder
  10.    ScaleHeight     =   3990
  11.    ScaleWidth      =   5190
  12.    Begin VB.TextBox txtFramesPerSecond 
  13.       Height          =   285
  14.       Left            =   1440
  15.       TabIndex        =   5
  16.       Text            =   "10"
  17.       Top             =   3600
  18.       Width           =   375
  19.    End
  20.    Begin VB.CommandButton cmdStart 
  21.       Caption         =   "Start"
  22.       Default         =   -1  'True
  23.       Height          =   495
  24.       Left            =   2160
  25.       TabIndex        =   1
  26.       Top             =   3480
  27.       Width           =   855
  28.    End
  29.    Begin VB.PictureBox picGraph 
  30.       Height          =   3375
  31.       Left            =   0
  32.       ScaleHeight     =   -100
  33.       ScaleLeft       =   0.5
  34.       ScaleMode       =   0  'User
  35.       ScaleTop        =   100
  36.       ScaleWidth      =   10.75
  37.       TabIndex        =   0
  38.       Top             =   0
  39.       Width           =   5175
  40.    End
  41.    Begin VB.Label Label1 
  42.       Caption         =   "Frames per second:"
  43.       Height          =   255
  44.       Index           =   1
  45.       Left            =   0
  46.       TabIndex        =   4
  47.       Top             =   3600
  48.       Width           =   1455
  49.    End
  50.    Begin VB.Label lblDay 
  51.       BorderStyle     =   1  'Fixed Single
  52.       Height          =   255
  53.       Left            =   4800
  54.       TabIndex        =   3
  55.       Top             =   3600
  56.       Width           =   375
  57.    End
  58.    Begin VB.Label Label1 
  59.       Caption         =   "Day:"
  60.       Height          =   255
  61.       Index           =   0
  62.       Left            =   4440
  63.       TabIndex        =   2
  64.       Top             =   3600
  65.       Width           =   375
  66.    End
  67. Attribute VB_Name = "frmStocks1"
  68. Attribute VB_GlobalNameSpace = False
  69. Attribute VB_Creatable = False
  70. Attribute VB_PredeclaredId = True
  71. Attribute VB_Exposed = False
  72. Option Explicit
  73. Private Const NUM_STOCKS = 10
  74. Private Const NUM_FRAMES = 100
  75. Private Data(1 To NUM_FRAMES, 1 To NUM_STOCKS) As Integer
  76. Private Playing As Boolean
  77. ' Generate some random data.
  78. Private Sub InitData()
  79. Dim stock As Integer
  80. Dim frame As Integer
  81.     ' Set the initial values between 30 and 70.
  82.     For stock = 1 To NUM_STOCKS
  83.         Data(1, stock) = Int(41 * Rnd + 30)
  84.     Next stock
  85.     ' Make values for the other frames.
  86.     ' Each value is up to +/- 5 different than the
  87.     ' previous value for the same stock.
  88.     For frame = 2 To NUM_FRAMES
  89.         For stock = 1 To NUM_STOCKS
  90.             Data(frame, stock) = _
  91.                 Data(frame - 1, stock) + _
  92.                 Int(11 * Rnd - 5)
  93.             If Data(frame, stock) < 0 Then _
  94.                 Data(frame, stock) = 5
  95.             If Data(frame, stock) > 100 Then _
  96.                 Data(frame, stock) = 95
  97.         Next stock
  98.     Next frame
  99. End Sub
  100. ' Start the animation.
  101. Private Sub cmdStart_Click()
  102.     If Playing Then
  103.         Playing = False
  104.         cmdStart.Caption = "Stopped"
  105.         cmdStart.Enabled = False
  106.     Else
  107.         cmdStart.Caption = "Stop"
  108.         Playing = True
  109.         InitData
  110.         PlayData
  111.         Playing = False
  112.         cmdStart.Caption = "Start"
  113.         cmdStart.Enabled = True
  114.     End If
  115. End Sub
  116. ' Play the animation.
  117. Private Sub PlayData()
  118. Dim milliseconds_per_frame As Long
  119. Dim frame As Integer
  120. Dim stock As Integer
  121. Dim next_time As Long
  122. Dim old_style As Integer
  123.     ' Set FillStyle to vbSolid.
  124.     old_style = picGraph.FillStyle
  125.     picGraph.FillStyle = vbSolid
  126.     picGraph.AutoRedraw = True
  127.     ' See how fast we should go.
  128.     If Not IsNumeric(txtFramesPerSecond.Text) Then _
  129.         txtFramesPerSecond.Text = "10"
  130.     milliseconds_per_frame = 1000 \ CLng(txtFramesPerSecond.Text)
  131.     ' Draw the background.
  132.     '
  133.     ' Note that we must cover all of the background
  134.     ' so it becomes part of the image. Then Cls
  135.     ' will restore it.
  136.     picGraph.Line (0, 0)-(NUM_STOCKS + 1.25, 50), RGB(128, 128, 128), BF
  137.     picGraph.Line (0, 50)-(NUM_STOCKS + 1.25, 100), picGraph.BackColor, BF
  138.     ' Make the picture a permanent part of the
  139.     ' background.
  140.     picGraph.Picture = picGraph.Image
  141.     ' Start the animation.
  142.     next_time = GetTickCount()
  143.     For frame = 1 To NUM_FRAMES
  144.         If Not Playing Then Exit For
  145.         ' Draw the graph.
  146.         picGraph.Cls
  147.         For stock = 1 To NUM_STOCKS
  148.             If Data(frame, stock) > 50 Then
  149.                 picGraph.Line (stock, 0)-(stock + 0.75, 50), vbRed, BF
  150.                 picGraph.Line (stock, 50)-(stock + 0.75, Data(frame, stock)), vbGreen, BF
  151.             Else
  152.                 picGraph.Line (stock, 0)-(stock + 0.75, Data(frame, stock)), vbRed, BF
  153.             End If
  154.         Next stock
  155.         picGraph.Line (0, 50)-(NUM_STOCKS + 1.25, 50), vbBlack, BF
  156.         lblDay.Caption = Format$(frame)
  157.         ' Wait until it's time for the next frame.
  158.         next_time = next_time + milliseconds_per_frame
  159.         WaitTill next_time
  160.     Next frame
  161.     ' Restore the old FillStyle.
  162.     picGraph.FillStyle = old_style
  163. End Sub
  164.